//
//  MSSearchViewController.m
//  MSJD
//
//  Created by 箭军刘 on 2018/10/13.
//  Copyright © 2018年 xiangbo. All rights reserved.
//

#import "MSSearchViewController.h"

#import "MSHomeFilterViewController.h"

#import "MSSearchHistoryCell.h"
#import "MSSearchResultCell.h"
#import "MSSearchDefaultView.h"
#import "MSSearchResultView.h"

#import "MSSearchDataSource.h"

@interface MSSearchViewController ()
<UITextFieldDelegate, UIGestureRecognizerDelegate, MSSearchDefaultViewDelegate>

/** 头部父view的高度 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSuperViewHeight;

@property (weak, nonatomic) IBOutlet UIView *searchSuperView;
@property (weak, nonatomic) IBOutlet UITextField *searchTextField; // 搜索框

@property (weak, nonatomic) IBOutlet UIView *searchShowView;

@property (nonatomic, strong) MSSearchDefaultView *searchDefaultView;

@property (nonatomic, copy) NSArray *searchHistoryList; // 历史搜索数据列表

@property (nonatomic, assign) NSInteger kind;
@property (nonatomic, copy) NSString *searchContent;

@end

@implementation MSSearchViewController {
    NSInteger _defaultSearchType; // 默认搜索类型 0：有默认搜索cell 1：没有默认搜索cell
}

+ (instancetype)loadSearchViewControllerWithKind:(NSInteger)kind title:(NSString *)title {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MSSearch" bundle:nil];
    MSSearchViewController *searchVC = [storyboard instantiateViewControllerWithIdentifier:@"MSSearchViewController"];
    searchVC.kind = kind;
    searchVC.searchContent = title;
    return searchVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initSubViews];
//    [self loadLocalSearchList];
    self.searchTextField.text = [MSSafeObject stringInfo:self.searchContent];
    [self.searchTextField becomeFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
}

#pragma mark - Init Method

- (void)initSubViews {
    // 搜索框的圆角
    self.searchSuperView.layer.cornerRadius = 16.0;
    // 搜索框的光标
    // 修改textfiled的光标颜色以及默认字体颜色
    self.searchTextField.tintColor = [UIColor colorWithHexString:@"#F05522"];
    [self.searchTextField setValue:[UIColor colorWithHexString:@"#9B9B9B"] forKeyPath:@"_placeholderLabel.textColor"];

    [self initTapGesture];
    
    MSSearchDefaultView *searchDefaultView = [MSSearchDefaultView loadSearchDefaultViewWithKind:2];
    searchDefaultView.customDelegate = self;
    [self.searchShowView addSubview:searchDefaultView];
    [searchDefaultView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.offset(0);
    }];
    self.searchDefaultView = searchDefaultView;
}

/** 初始化点击手势 */
- (void)initTapGesture {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenKeyboardTap:)];
    tap.delegate = self;
    [self.view addGestureRecognizer:tap];
}

#pragma mark - System Method

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    
    [self.view endEditing:YES];
}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    NSString *touchView = NSStringFromClass([touch.view class]);
    // 点击的是空白处时响应隐藏键盘点击
    if ([touchView isEqualToString:@"UITableView"] ||
        [touchView isEqualToString:@"UIView"]) {
        return YES;
    }
    return NO;
}

#pragma mark - UITextFieldDelegate

// 键盘的return按钮点击
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if ([MSSafeObject isNull:textField.text]) {
        return NO;
    }
    // 关闭键盘
    [textField resignFirstResponder];
    // 查询数据
    
    // 本地数据变更
    [MSSearchDataSource saveSearchContent:textField.text];
    // 更新本地数据(临时)
    [self didSelectSearchItem:textField.text];
    return YES;
}

#pragma mark - MSSearchDefaultViewDelegate
/** 选中了某个内容 */
- (void)didSelectSearchItem:(NSString *)searchContent {
    
    // 本地数据变更
    [MSSearchDataSource saveSearchContent:searchContent];
    
    NSLog(@"选中了搜索内容:%@", searchContent);
    if (self.SearchButtonClickBlock) {
        self.SearchButtonClickBlock(searchContent);
//        if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:NO];
    } else {
        MSHomeFilterViewController *homeFilterVC = [MSHomeFilterViewController loadHomeFilterViewControllerWithKind:self.kind IsfromSearchVC:YES searchContent:searchContent filterInfo:nil];
        [self.navigationController pushViewController:homeFilterVC animated:YES];
    }
}

#pragma mark - XIB Response
/** 取消按钮点击 */
- (IBAction)cancelButtonClick:(id)sender {
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:NO];
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Event Response

/** 隐藏键盘 */
- (void)hiddenKeyboardTap:(UIGestureRecognizer *)gesture {
    [self.view endEditing:YES];
}

#pragma mark -  Memory Method

- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end
